﻿using System;
using System.Collections.Generic;
using System.Linq;

#if !WINRT
using System.Windows;
using System.Windows.Controls;
#else

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
#endif

namespace MishraReader
{
    /// <summary>
    ///   A ContentPresenter that sits in the logical tree and can find the DataTemplate for any given item as if that item were in the same logical tree.
    /// </summary>
    internal class TemplateFinder : ContentPresenter
    {
        public TemplateFinder()
        {
            //Hide ourselves because we don't want to show or take up space.
            //We're only here to find DataTemplates not show anything.
            Visibility = Visibility.Collapsed;
        }

        public DataTemplate FindTemplate(object item)
        {
            //Temporarily insert the content so we can find the DataTemplate we would use
            //If we were actually displaying.
            Content = item;
            
#if !WINRT
            var template = base.ChooseTemplate();
#else
            var template = base.ContentTemplate;
#endif
            //Remove the content so we don't actually display anything.
            Content = null;
            return template;
        }
    }
}